Passed
Pull Request — master (#215)
by
unknown
04:50
created

CreateContactCommandHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 18
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 11 2
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { Contact } from 'src/Domain/Contact/Contact.entity';
4
import { EmptyContactException } from 'src/Domain/Contact/Exception/EmptyContactException';
5
import { IContactRepository } from 'src/Domain/Contact/Repository/IContactRepository';
6
import { CreateContactCommand } from './CreateContactCommand';
7
8
@CommandHandler(CreateContactCommand)
9
export class CreateContactCommandHandler {
10
  constructor(
11
    @Inject('IContactRepository')
12
    private readonly contactRepository: IContactRepository
13
  ) {}
14
15
  public async execute(command: CreateContactCommand): Promise<string> {
16
    const { firstName, lastName, company, phoneNumber, email, notes } = command;
17
18
    if (!firstName && !lastName && !company) {
19
      throw new EmptyContactException();
20
    }
21
22
    return (
23
      await this.contactRepository.save(
24
        new Contact(firstName, lastName, company, email, phoneNumber, notes)
25
      )
26
    ).getId();
27
  }
28
}
29